package com.zdft.promotion.common.model;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 抽象过滤条件
 */
public class AbstractFilterModel {
    private final Logger log = LoggerFactory.getLogger(getClass());

    /**
     * 是否支持分页
     */
    private boolean pageable = true;

    private int page     = 1;
    private int pageSize = 20;

    /**
     * 排序列表
     */
    private List<Sort> sortList;

    public boolean isPageable() {
        return pageable;
    }

    public void setPageable(boolean pageable) {
        this.pageable = pageable;
    }

    public int getMinRowNum() {
        return (page - 1) * pageSize + 1;
    }

    public int getMaxRowNum() {
        return page * pageSize;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        if (page < 1) {
            this.page = 1;
            log.warn("Offset less than 1, set default:1!");
        } else {
            this.page = page;
        }
    }

    public void setPageSize(int pageSize) {
        if (pageSize <= 0 || pageSize > 100) {
            log.warn("Limit less than 0 or larger than 100, set default:100!");
            this.pageSize = 100;//默认100
        } else {
            this.pageSize = pageSize;
        }
    }

    public int getPageSize() {
        return pageSize;
    }

    public List<Sort> getSortList() {
        return sortList;
    }

    public void setSortList(List<Sort> sortList) {
        this.sortList = sortList;
    }

    /**
     * 添加排序
     *
     * @param sort 排序
     */
    public void addSort(Sort sort) {
        if (sortList == null) {
            sortList = new ArrayList<>();
        }
        sortList.add(sort);
    }

}
